﻿using Module.FlowChart.Logic;
using System.Drawing;

namespace Module.FlowChart.Model
{
    internal sealed class RunAbleFCWorkPage : FCWorkPageBase
    {


        static public string TypeNameS = typeof(RunAbleFCWorkPage).Name;

        public override string TypeName => TypeNameS;

        static void StaticInit()
        {
            string tname = TypeNameS;
            DefaultPageType = tname;
            WorkPageLoaders[tname] = (br) =>
            {
                RunAbleFCWorkPage re = new RunAbleFCWorkPage();
                re.LoadFromBytes(br);
                return re;
            };
            WorkPageCreators[tname] = (name) =>
            {
                return new RunAbleFCWorkPage(name);
            };
            WorkPageNameCreator[tname] = (index) =>
            {
                return index == 1 ? "Main" : "Func" + (index - 1);
            };
        }

        public override int AddLine(FCLine line)
        {
            IsChanged = true;
            line.Id = ++LineIdPointer;
            Lines[LineIdPointer] = line;
            FCNode node1 = Nodes[line.From];
            int removes = 0;
            if (line.Link1)
            {
                if (node1.Next != 0)
                {
                    foreach (var line2 in Lines.Values)
                    {
                        if (line2.Link1 && line2.From == node1.Id && line2.Id != line.Id)
                        {
                            removes = line2.Id;
                            break;
                        }
                    }
                    if (removes > 0)
                    {
                        Lines.Remove(removes);
                    }
                    node1.Next = line.To;
                }
                else
                {
                    node1.Next = line.To;
                }
            }
            else
            {
                if (node1.FalseNext != 0)
                {
                    node1.FalseNext = line.To;
                    foreach (var line2 in Lines.Values)
                    {
                        if (!line2.Link1 && line2.From == node1.Id && line2.Id != line.Id)
                        {
                            removes = line2.Id;
                            break;
                        }
                    }
                    if (removes > 0)
                    {
                        Lines.Remove(removes);
                    }
                }
                else
                {
                    node1.FalseNext = line.To;
                }
            }
            return line.Id;
        }
        public override string RemoveLine(int id)
        {
            IsChanged = true;
            if (!Lines.ContainsKey(id))
            {
                return ("没有这个线段");
            }
            var line = Lines[id];
            Lines.Remove(id);
            //修改与之关联的节点连接
            if (Nodes.ContainsKey(line.From))
            {
                var node = Nodes[line.From];
                if (line.Link1)
                {
                    node.TrueNext = 0;
                }
                else
                {
                    node.FalseNext = 0;
                }
            }
            return "";
        }

        public RunAbleFCWorkPage()
        {

        }
        public RunAbleFCWorkPage(string name) : base(name)
        {
            FCNode node1 = Nodes[AddNode(new FCNode(new Rectangle(100, 100, 100, 100), true, name))];
            FCNode node2 = Nodes[AddNode(new FCNode(new Rectangle(400, 100, 100, 100), false, ""))];
            AddLine(new FCLine(node1.Id, node2.Id, 3, 1,
                LineManager.GetLinePoints(
                    node1.LogicRect, 3,
                    node2.LogicRect, 1)
                )
           );
        }

        protected override void _OnChangeName(string from, string to)
        {
            //修改函数名称
            FindNode(1).FuncName = to;
        }

    }

}

